package com.project2.controller;


import com.github.pagehelper.util.StringUtil;
import com.project2.bean.CommonData;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.CreatGoodsBo;
import com.project2.bean.bo.GoodsPageBo;
import com.project2.bean.po.MarketGoods;
import com.project2.bean.vo.CatAdnBrand;
import com.project2.bean.vo.GoodsDetailVo;
import com.project2.service.AdminGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {


    @Autowired
    AdminGoodsService adminGoodsService;

    /*
    TODO 商品回显与查找
     */
    @RequestMapping("list")
    public ResultVo goods(GoodsPageBo goods){
        CommonData<MarketGoods> marketGoodsCommonData = adminGoodsService.selectGoods(goods);
        return ResultVo.ok(marketGoodsCommonData);
    }

    /*
    TODO 制造商与分类
     */
    @RequestMapping("catAndBrand")
    public ResultVo catAndBrand(){
        CatAdnBrand catAdnBrand = adminGoodsService.selectCatAndBrand();
        return ResultVo.ok(catAdnBrand);
    }

    /*
    TODO 添加商品 缺少参数
     */
    @RequestMapping("create")
    public ResultVo createGoods(@RequestBody CreatGoodsBo creatGoods){
        MarketGoods goods = creatGoods.getGoods();
        if (StringUtil.isEmpty(goods.getName())||StringUtil.isEmpty(goods.getGoodsSn())){
            return ResultVo.ok("参数不正确",401);
        }

        try {
            Integer.valueOf(goods.getGoodsSn());
        } catch (NumberFormatException e) {
            return ResultVo.ok("商品编号必须为纯数字",401);
        }


        String name = goods.getName();
        String count="";
        for (int i = 0; i <name.length() ; i++) {
            count+=" ";
        }
        if (name.equals(count)){
            return ResultVo.ok("参数不正确",401);
        }

        ResultVo resultVo = adminGoodsService.insertGoods(creatGoods);
        return resultVo;
    }

    /*
    TODO 编辑商品之商品回显
     */
    @RequestMapping("detail")
    public ResultVo detail(Integer id){
        GoodsDetailVo goodsDetailVo = adminGoodsService.selectGoodsMsg(id);
        return ResultVo.ok(goodsDetailVo);
    }

    /*
      TODO 编辑商品之编辑
     */
    @RequestMapping("update")
    public ResultVo updateGoods(@RequestBody CreatGoodsBo creatGoods){
        ResultVo resultVo = adminGoodsService.updateGoods(creatGoods);
        return resultVo;

    }


    /*
    TODO 删除商品 已完成
     */
    @RequestMapping("delete")
    public ResultVo deleteGoods(@RequestBody MarketGoods goods){
        ResultVo resultVo = adminGoodsService.deleteGoods(goods);
        return resultVo;
    }


}
